<?php
require_once('../../../system/common.inc.php');
$packages = DB::fetch_all("SELECT `ID`, `Name`,`Icon2`, `Section`, `SectionID`, `Description`,`DownloadTimes`,`Package` FROM `".DCRM_CON_PREFIX."Packages` WHERE (`Stat` = '1') ORDER BY `CreateStamp` DESC LIMIT 1");
foreach($packages as $package) {
  $package=$package['Name'];   }
$packages2 = DB::fetch_all("SELECT `ID`, `Name`,`Icon2`, `Section`, `SectionID`, `Description`,`DownloadTimes`,`Package` FROM `".DCRM_CON_PREFIX."Packages` WHERE (`Stat` = '1') ORDER BY `CreateStamp` DESC LIMIT 2");
foreach($packages2 as $package2) {
  $package2=$package2['Name'];   }     
$packages3 = DB::fetch_all("SELECT `ID`, `Name`,`Icon2`, `Section`, `SectionID`, `Description`,`DownloadTimes`,`Package` FROM `".DCRM_CON_PREFIX."Packages` WHERE (`Stat` = '1') ORDER BY `CreateStamp` DESC LIMIT 3");
foreach($packages3 as $package3) {
  $package3=$package3['Name'];   } 
$packages4 = DB::fetch_all("SELECT `ID`, `Name`,`Icon2`, `Section`, `SectionID`, `Description`,`DownloadTimes`,`Package` FROM `".DCRM_CON_PREFIX."Packages` WHERE (`Stat` = '1') ORDER BY `CreateStamp` DESC LIMIT 4");
foreach($packages4 as $package4) {
  $package4=$package4['Name'];   } 
$packages5 = DB::fetch_all("SELECT `ID`, `Name`,`Icon2`, `Section`, `SectionID`, `Description`,`DownloadTimes`,`Package` FROM `".DCRM_CON_PREFIX."Packages` WHERE (`Stat` = '1') ORDER BY `CreateStamp` DESC LIMIT 5");
foreach($packages5 as $package5) {
  $package5=$package5['Name'];   }  

$appid = "wx62a1e0246db01c6f";  
$appsecret = "4c2af1e041fed0ad3ad62acc3f3621cc";  
 $times= date('y年m月d日 H:i:s',time());
echo "<p>群发时间：'".$times."'</p></br>"; 
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret;//取得token
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
$jsoninfo = json_decode($output, true);
$access_token = $jsoninfo["access_token"];
echo $access_token."</br>";
$url= "https://api.weixin.qq.com/cgi-bin/user/info?access_token=14_H1gLClr8GM7qNYjS4oMQpiF2qslLyZoky4zGk8ABGaCovTRujY0gV0x4TTgGFAgEFTDmd24LFma8X3o-rlsjtWDsB2QlKSFMY-8KcdFuDJXiT0sDShALf0lhjI8WyxaLdxqKeoTDQWF77qGoBGJfAJAPUX&openid=oq6ln1GnjJXkGoStLK9vQ2NpX58M"; 

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
$array = json_decode($output, true);
//echo $array["count"]."</br>";
 foreach ($array["Content-Length"]["nickname"] as $key=>&$value) {//获取用户的唯一标识
     echo ($key)."->".$value."</br>" ;
 }  
/*$ACCESS_TOKEN = $access_token;
$data=array( 
'touser'=>"$value", //要发送给用户的openid
'template_id'=>"vt1GKrEEVJzjT4qMn6WL5NsZssix-g6MyhfayGwFDgk",//改成自己的模板id，在微信后台模板消息里查看
'url'=>"http://mycydia.vip/gxtz.php", //自己网站链接url 
'data'=>array(
  'first'=>array(
    'value'=>"布斯中文推送源™插件更新通知",
    'color'=>"#743A3A"
    ),
  'keyword1'=>array(
    'value'=>urlencode("尊敬的布斯中文推送源™用户！本次更新的插件是以下几款\\n$package\\n$package2\\n$package3\\n$package4\\n$package5\\n等一系列插件！详细请点击详情查看！"),
    'color'=>"#f00"
    ), 
  'keyword2'=>array(
    'value'=>$times,
    'color'=>"#173177"
    ), 
  'remark'=>array(
    'value'=>"感谢您使用布斯中文推送源™！祝您生活愉快!",
    'color'=>"#3d3d3d"
    ),
)
);


//$json_data=json_encode($data);//转化成json数组让微信可以接收
//$url="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$ACCESS_TOKEN;//模板消息请求URL
//$res=https_request($url,urldecode($json_data));//请求开始


 //var_dump($template)."</br>";

 //echo urldecode(json_encode($template))."</br>";
$url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$access_token;
$res = https_request($url, urldecode(json_encode($data)));//"";//群发命令
$res=json_decode($res,true);
// var_dump($res)."</br>";
 }

if($res['errcode']==0 && $res['errcode']=="ok"){
  echo "用户OPENID：".$data["touser"]."--发送成功";  
}


//curl请求函数，微信都是通过该函数请求
function https_request($url,$data = null){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
if (!empty($data)){
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($curl);
curl_close($curl);
return $output;
}*/
?>